package com.nhjf.mobile.controller.internalCourses;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.coursesPlayback.CoursesPlayback;
import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.coursesPlayback.CoursesPlaybackQo;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.coursesPlayback.CoursesPlaybackService;
import com.nhjf.service.coursesSeries.CoursesSeriesService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.CoursesCollectionService;
import com.nhjf.service.teachers.CoursesCommentService;
import com.nhjf.service.teachers.CoursesService;
import com.nhjf.service.teachers.OrganizationHasStudentService;
import com.nhjf.service.teachers.RecentlyStudiedService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/internalCourses")
@Controller
public class InternalCoursesController {
	@Autowired
	private CoursesSeriesService coursesSeriesService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private OrganizationHasStudentService osdService; 

	
	/**
	 * 课程回放列表
	 * @param teacherId
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/internalCoursesPage")
	public String coursesPlayBackTeacherpage(@RequestParam(value = "page", defaultValue = "1")Integer page,Long typeOne,Long typeTwo,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(user.getIsRole()==2) {
					StudentQo qo1= new StudentQo();
					qo1.setSeachStudent(true);
					qo1.setUserid(user.getId());
					Student student=studentService.queryUnique(qo1);
					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
					hasStudentQo.setStudentId(student.getId());
					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
					if(hasStudent!=null) {
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						CoursesSeriesQo qo=new CoursesSeriesQo();
						qo.setOrganizationId(hasStudent.getOrganization().getId());
//						qo.setUploader(Long.parseLong(teacherId));
						qo.setSeachTeacher(true);
						qo.setCourseShare(1);
						if(typeOne!=null){
							qo.setCourseTypeOne(typeOne);
						}
						if(typeTwo!=null){
							qo.setCourseTypeTwo(typeTwo);
						}
						pagination.setCondition(qo);
						
						pagination.setPageNo(page);
						pagination = coursesSeriesService.queryPagination(pagination);
						List<CoursesSeries> coursesSeriesList=(List<CoursesSeries>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesSeries coursesSeries:coursesSeriesList) {
							if(StringUtils.isNotEmpty(coursesSeries.getUploader().getAvatar())) {
								if(coursesSeries.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									coursesSeries.getUploader().setAvatar(imagePath+"/picture/"+coursesSeries.getUploader().getAvatar());
								}
							}
							coursesSeries.setCover(imagePath+"/img/"+coursesSeries.getCover());
							coursesSeries.setCourseTypeOne(null);
							coursesSeries.setCourseTypeTwo(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					TeachersQo tqo=new TeachersQo();
					tqo.setSeachOrganization(true);
					tqo.setSeachUser(true);
					tqo.setUserId(user.getId());
					Teacher teach=teacherService.queryUnique(tqo);
					if(teach!=null) {
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						CoursesSeriesQo qo=new CoursesSeriesQo();
						qo.setOrganizationId(teach.getTraininginstitution().getId());
//						qo.setUploader(Long.parseLong(teacherId));
						qo.setSeachTeacher(true);
						qo.setCourseShare(1);
						if(typeOne!=null){
							qo.setCourseTypeOne(typeOne);
						}
						if(typeTwo!=null){
							qo.setCourseTypeTwo(typeTwo);
						}
						pagination.setCondition(qo);
						pagination.setPageNo(page);
						pagination = coursesSeriesService.queryPagination(pagination);
						List<CoursesSeries> coursesSeriesList=(List<CoursesSeries>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesSeries coursesSeries:coursesSeriesList) {
							if(StringUtils.isNotEmpty(coursesSeries.getUploader().getAvatar())) {
								if(coursesSeries.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									coursesSeries.getUploader().setAvatar(imagePath+"/picture/"+coursesSeries.getUploader().getAvatar());
								}
							}
							coursesSeries.setCover(imagePath+"/img/"+coursesSeries.getCover());
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}

		}

	}

}
